/**
 * 人才库-推荐到岗位modal
 */
import React from 'react';
import { Modal, Form, Input, Select, Spin, Checkbox } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import { getEmployeeInfoList } from '@/services/components/userform';
import { queryRecommendPositionList } from '../../service';
import styles from './index.less';

const FORM_DEFAULT_LAYOUT = {
    labelCol: {
        span: 5
    },
    wrapperCol: {
        span: 17
    }
};
const { TextArea } = Input;
const { Option } = Select;
const MoreOperateModal = ({ form, resumeId, onOk, visible = false, destroyOnClose = true, recommendType, ...rest }) => {
    // const user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};
    // const { companyId } = user;
    const { getFieldDecorator, validateFields, resetFields } = form;
    // 岗位列表
    const { data: postList } = useRequest(
        () => {
            if (visible && resumeId) return queryRecommendPositionList(resumeId);
            return Promise.resolve({ code: 10000, data: [] });
        },
        {
            initialData: [],
            refreshDeps: [resumeId, visible],
            ...defaultFormatResult
        }
    );
    // 获取人员
    const { data: employeeList, loading: employeeLoading } = useRequest(
        () => {
            if (visible) {
                // return getEmployeeInfoList({
                //     getChildByOrganizationCodes: [companyId]
                // });
                return getEmployeeInfoList();
            }
            return Promise.resolve({ code: 10000, data: [] });
        },
        {
            refreshDeps: [visible],
            initialData: [],
            ...defaultFormatResult
        }
    );
    const currRecommendRecruitId = form.getFieldValue('recommendRecruitId');
    // 通知人员赋值
    React.useEffect(() => {
        if (!currRecommendRecruitId || !postList || !postList.length) {
            return;
        }
        const target = postList.find(item => item.recruitId === currRecommendRecruitId);
        if (!target) {
            return;
        }
        const { launchEmployeeCode, principalEmployeeCode } = target;
        form.setFieldsValue({
            noticeEmployeeCodeList: [...new Set([launchEmployeeCode, principalEmployeeCode])].filter(_ => !!_)
        });
    }, [currRecommendRecruitId, postList]);
    const [submitLoading, setSubmitLoading] = React.useState(false);
    const handleOk = () => {
        validateFields((err, values) => {
            console.log(values);
            if (err) {
                return;
            }
            setSubmitLoading(true);
        });
    };
    return (
        <Modal
            {...rest}
            title={recommendType === 'other' ? '推荐到其他岗位' : '推荐到岗位'}
            visible={visible}
            width={720}
            onOk={handleOk}
            confirmLoading={submitLoading}
            destroyOnClose={destroyOnClose}
            afterClose={() => {
                resetFields();
            }}
            className={styles.modalWrap}
        >
            <Spin spinning={employeeLoading}>
                <Form {...FORM_DEFAULT_LAYOUT} className={styles.modalFormWrap}>
                    <Form.Item label="推荐岗位">
                        <div className={styles.recommendJob}>
                            {getFieldDecorator('recommendRecruitId', {
                                rules: [
                                    {
                                        required: true,
                                        message: '推荐岗位不能为空'
                                    }
                                ]
                            })(
                                <Select
                                    showSearch
                                    style={{ width: 285 }}
                                    placeholder="请选择推荐岗位 - 部门（招聘负责人）"
                                    optionFilterProp="children"
                                    getPopupContainer={triggerNode => triggerNode.parentElement}
                                    dropdownMatchSelectWidth={false}
                                    dropdownStyle={{ maxWidth: 550 }}
                                >
                                    {postList?.map(item => (
                                        <Option value={item.recruitId} key={item.recruitId}>
                                            {`${item.name}（${item.principalEmployeeName || '--'}）`}
                                        </Option>
                                    ))}
                                </Select>
                            )}
                            {recommendType === 'other' &&
                                getFieldDecorator('agreement', {
                                    valuePropName: 'checked'
                                })(<Checkbox>从当前应聘岗位中淘汰</Checkbox>)}
                        </div>
                    </Form.Item>
                    <Form.Item label="通知人员">
                        {getFieldDecorator(
                            'noticeEmployeeCodeList',
                            {}
                        )(
                            <Select
                                mode="multiple"
                                style={{ width: '100%' }}
                                placeholder="请选择通知人员"
                                allowClear
                                optionFilterProp="children"
                                getPopupContainer={triggerNode => triggerNode.parentElement}
                            >
                                {employeeList?.map(item => (
                                    <Option value={item.employeeCode} key={item.employeeCode}>
                                        {item.employeeName}
                                    </Option>
                                ))}
                            </Select>
                        )}
                    </Form.Item>
                    <Form.Item label="理由">
                        {getFieldDecorator('reason', {
                            rules: [
                                {
                                    required: true,
                                    message: '理由不能为空'
                                }
                            ]
                        })(<TextArea placeholder="请输入理由" autoSize={{ minRows: 3, maxRows: 6 }} maxLength={800} />)}
                    </Form.Item>
                </Form>
            </Spin>
        </Modal>
    );
};

export default Form.create()(MoreOperateModal);
